<?php

class m120503_072342_create_reply extends CDbMigration {

	public function safeUp() {
		$reply = array(
			'id'=>'pk',
			'postId'=>'integer',
			'userId'=>'integer',
			'name'=>'string',
			'email'=>'varchar(128)',
			'value'=>'text',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('replies', $reply);
		$this->createIndex('index_reply_post', 'replies', 'postId');
		$this->createIndex('index_reply_user', 'replies', 'userId');
		$this->createIndex(
			'index_reply_updated_at', 'replies', 'updatedAt'
		);

		$auth = Yii::app()->authManager;

		$auth->createOperation('replyUpdate', 'Update Reply');
		$auth->createOperation('replyDelete', 'Delete Reply');
		$auth->createOperation('replyManage', 'Manage Reply');

		$role = $auth->createRole('replyModerator', 'Reply Moderator');
		$role->addChild('replyUpdate');
		$role->addChild('replyDelete');
		$role->addChild('replyManage');

		$auth->addItemChild('admin', 'replyModerator');
	}

	public function safeDown() {
		$this->dropTable('replies');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'replyModerator');

		$auth->removeItemChild('replyModerator', 'replyUpdate');
		$auth->removeItemChild('replyModerator', 'replyDelete');
		$auth->removeItemChild('replyModerator', 'replyManage');

		$auth->removeAuthItem('replyUpdate');
		$auth->removeAuthItem('replyDelete');
		$auth->removeAuthItem('replyManage');

		$auth->removeAuthItem('replyModerator');
	}

}